package com.qf.fmall.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.entity.Category;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.entity.ProductImg;
import com.qf.fmall.index.mapper.CategoryMapper;
import com.qf.fmall.index.mapper.ProductImgMapper;
import com.qf.fmall.index.mapper.ProductMapper;
import com.qf.fmall.index.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author weijy
 * @since 2024-01-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductImgMapper productImgMapper;


    @Override
    public List<Category> getCategoryList() {

        List<Category> categoryList =
                categoryMapper.selectList(new QueryWrapper<Category>()
                        .eq("category_level", 1)
                        .orderByAsc("category_id"));

        for (Category category : categoryList) {

            List<Category> secondList = categoryMapper.selectList(new QueryWrapper<Category>()
                    .eq("category_level", 2)
                    .eq("parent_id", category.getCategoryId())
                    .orderByAsc("category_id"));
            category.setCategories(secondList);

            for (Category category_ : secondList) {
                List<Category> thirdList = categoryMapper.selectList(new QueryWrapper<Category>()
                        .eq("category_level", 3)
                        .eq("parent_id", category_.getCategoryId())
                        .orderByAsc("category_id"));

                category_.setCategories(thirdList);
            }
        }


        return categoryList;
    }

    @Override
    public List<Category> categoryRecommendsList() {

        List<Category> categoryList =
                categoryMapper.selectList(new QueryWrapper<Category>()
                        .eq("category_level", 1));

        for (Category category : categoryList) {
            List<Product> productList =
                    productMapper.selectList(new QueryWrapper<Product>()
                            .eq("root_category_id", category.getCategoryId()));

            category.setProducts(productList);

            for (Product product : productList) {
                List<ProductImg> secondList =
                        productImgMapper.selectList(new QueryWrapper<ProductImg>()
                                .eq("product_id", product.getProductId()));

                if (secondList != null) {
                    product.setImgs(secondList);
                }
            }
        }


        return categoryList;
    }

    @Override
    public List<Category> getCategoryListPro() {

        // 1.查询所有分类数据
        List<Category> allData = list();

        // 如何让 allData 中的每一个分类对象中的 categories 属性被正确赋值呢？
        // 构建一个 Map，key为 分类对象的id，value为 分类对象本身
        HashMap<Integer, Category> hashMap = new HashMap<>();

        for (Category category : allData) {
            Integer categoryId = category.getCategoryId();
            hashMap.put(categoryId, category);
        }

        ArrayList<Category> categories = new ArrayList<>();
        for (Category category : allData) {

            Integer parentId = category.getParentId();
            if (parentId == 0) {
                categories.add(category);
            } else {
                Category parent = hashMap.get(parentId);
                List<Category> categoryList = parent.getCategories();
                categoryList.add(category);
            }


        }

        return categories;
    }


}
